package scau.erps.service.impl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.service.IdEntityService;

import scau.erps.app.Erps;
import scau.erps.entity.Research;
import scau.erps.entity.Sand;
import scau.erps.service.ResearchService;

/**
 * 研发服务实现类
 * @author DCJ
 *
 */
public class ResearchServiceImpl extends IdEntityService<Research> implements ResearchService {

	@Override
	public void researchInvest(Research research) {
		int amount = research.getAmount();
		Sand sand = Erps.getSand();
		int cash = sand.getCash();
		sand.setCash(cash-amount);
		this.dao().update(sand);
		this.dao().insert(research);		
	}
	/**
	 * 得到研发投资次数
	 * @param team_Id
	 * @param type
	 * @return
	 */
	public int findResearchs(int team_Id,String type){
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(team_Id != 0){
			e1 = Cnd.exps("team_Id", "=", team_Id);			
		}
		if(type != null){
			e2 = Cnd.exps("type", "=",type);
		}
		int i = this.query(Cnd.where(e1).and(e2), null).size();
		return i;
	}
	/**
	 * 通过team_Id来查找小组的研发情况
	 * @author FYJ
	 */
	public List<Research> getMaterial(int team_Id, String time) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(team_Id != 0){
			e1 = Cnd.exps("team_Id", "=", team_Id);			
		}
		if(time != null){
			e2 = Cnd.exps("time", "=", time);
		}
		return this.query(Cnd.where(e1).and(e2), null);
	}
	@Override
	public boolean getResearchStatus(int team, String type) {
		if ("Crystal".equals(type))  
		  if  (findResearchs(team, "Crystal") == 4) return true;
		                                       else return false;
		
		if ("Ruby".equals(type)) 
		  if  (findResearchs(team, "Ruby") == 6) return true;
				                               else return false;	
		
		if ("Saphire".equals(type)) 
		  if  (findResearchs(team, "Saphire") == 8) return true;
			                                   else return false;		

		if ("ISO9000".equals(type)) 
		  if  (findResearchs(team, "ISO9000") == 1) return true;
				                               else return false;			
		
		if ("ISO14000".equals(type)) 
		  if  (findResearchs(team, "ISO14000") == 2) return true;
					                           else return false;			
		
		if ("区域".equals(type)) 
		  if  (findResearchs(team, "区域") == 1) return true;
					                           else return false;				

		if ("国内".equals(type)) 
		  if  (findResearchs(team, "国内") == 2) return true;
					                           else return false;			

		if ("亚洲".equals(type)) 
		  if  (findResearchs(team, "亚洲") == 3) return true;
					                           else return false;	

		if ("国际".equals(type)) 
		  if  (findResearchs(team, "国际") == 4) return true;
					                           else return false;	
		return false;

	}
	@Override
	public void researchInvest(String type){
		if("Crystal".equals(type)){
			Research crystalResearch = new Research();
			crystalResearch.setTeam_Id(Erps.getCurrentTeam().getId());
			crystalResearch.setType("Crystal");
			crystalResearch.setTime(Erps.getSand().getTime());
			crystalResearch.setAmount(1);
			crystalResearch.setName("产品研发");
			this.researchInvest(crystalResearch);
		}
		if("Ruby".equals(type)){
			Research rubyResearch = new Research();
			rubyResearch.setTeam_Id(Erps.getCurrentTeam().getId());
			rubyResearch.setType("Ruby");
			rubyResearch.setTime(Erps.getSand().getTime());
			rubyResearch.setAmount(2);
			rubyResearch.setName("产品研发");
			this.researchInvest(rubyResearch);
		}
		if("Sapphire".equals(type)){
			Research sapphireResearch = new Research();
			sapphireResearch.setTeam_Id(Erps.getCurrentTeam().getId());
			sapphireResearch.setType("Sapphire");
			sapphireResearch.setTime(Erps.getSand().getTime());
			sapphireResearch.setAmount(2);
			sapphireResearch.setName("产品研发");
			this.researchInvest(sapphireResearch);
		}
		if ("区域".equals(type)) {
			Research districtResearch = new Research();
			districtResearch.setTeam_Id(Erps.getCurrentTeam().getId());
			districtResearch.setType("区域");
			districtResearch.setTime(Erps.getSand().getTime());
			districtResearch.setAmount(1);
			districtResearch.setName("市场开拓");
			this.researchInvest(districtResearch);
			
		}
		if ("国内".equals(type)) {
			Research domesticResearch = new Research();
			domesticResearch.setTeam_Id(Erps.getCurrentTeam().getId());
			domesticResearch.setType("国内");
			domesticResearch.setTime(Erps.getSand().getTime());
			domesticResearch.setAmount(1);
			domesticResearch.setName("市场开拓");
			this.researchInvest(domesticResearch);
			
		}
		if ("亚洲".equals(type)) {
			Research asiaResearch = new Research();
			asiaResearch.setTeam_Id(Erps.getCurrentTeam().getId());
			asiaResearch.setType("亚洲");
			asiaResearch.setTime(Erps.getSand().getTime());
			asiaResearch.setAmount(1);
			asiaResearch.setName("市场开拓");
			this.researchInvest(asiaResearch);
		}
		if ("国际".equals(type)) {
			Research internationResearch = new Research();
			internationResearch.setTeam_Id(Erps.getCurrentTeam().getId());
			internationResearch.setType("国际");
			internationResearch.setTime(Erps.getSand().getTime());
			internationResearch.setAmount(1);
			internationResearch.setName("市场开拓");
			this.researchInvest(internationResearch);			
		}
		if ("ISO9000".equals(type)) {
			Research iso9000Research = new Research();
			iso9000Research.setTeam_Id(Erps.getCurrentTeam().getId());
			iso9000Research.setType("ISO9000");
			iso9000Research.setTime(Erps.getSand().getTime());
			iso9000Research.setAmount(1);
			iso9000Research.setName("ISO认证");
			this.researchInvest(iso9000Research);
			
		}
		if ("ISO14000".equals(type)) {
			Research iso14000Research = new Research();
			iso14000Research.setTeam_Id(Erps.getCurrentTeam().getId());
			iso14000Research.setType("ISO14000");
			iso14000Research.setTime(Erps.getSand().getTime());
			iso14000Research.setAmount(1);
			iso14000Research.setName("ISO认证");
			this.researchInvest(iso14000Research);
		}
	}
}
